<?php
/**
 * User: @lose
 * Date: 2020/7/28
 * Time: 2:02 下午
 */

namespace Beerus\Modules\BlackCard\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\BlackCard\BlackCategorySort;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Grid;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Form;
use Illuminate\Support\MessageBag;
use Doraemon\model\Category as mCategory;


class CategoryController extends Controller
{

    public function categoryList(Content $content, Request $request)
    {
        //问答列表
        $category_list = (array)BlackCategorySort::getCategorys();
        $count = (int)count($category_list);
        $gridModel = new pGridDataModel($category_list, $count, []);
        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableFilter();
            $grid->disablePagination();

            $grid->column('id', 'id')->sort();
            $grid->column('category_id', 'category_id');
            $grid->column("aaa", '分类名')->display(function () {
                $category_info = mCategory::selectByWhere([
                    'id' => $this->category_id
                ], ['id', 'cate_name(title)']);
                return ($category_info) ? $category_info[0]['title'] : '';
            });

        });

        return $content
            ->header('黑卡分类管理')
            ->body($listGrid);

    }

    //添加黑卡分类
    public function addCategory(Request $request, Content $content)
    {
        $request = $request->all();
        return $content
            ->header('添加黑卡分类')
            ->body(self::editCategoryAction($request, $id = 0));

    }

    //修改黑卡分类
    public function editCategory(Request $request, Content $content, $id)
    {
        $request = $request->all();
        return $content
            ->header('修改黑卡分类')
            ->body(self::editCategoryAction($request, $id));


    }

    public function editCategoryAction($request, $id = 0)
    {
        $data = [];
        if ($id != 0) {
            //修改
            $data = BlackCategorySort::getById($id);
            $form = new Form($data);
            $form->hidden('id');
        } else {
            //添加
            $form = new Form($data);
        }
        $form->text('category_id', '分类id')->required('不能为空');
        $form->number('category_sort', '黑卡分类排序')->help('黑卡分类排序排序，会按照由小到大的顺序在前端展示')->default(0);
        $form->action('/admin/blackCard/categorysort/save');
        return $form->render();
    }

    //提交
    public function saveCategory(Request $request, Content $content)
    {
        $request = $request->all();
        $rs = false;
        $data['category_id'] = $request['category_id'];
        $category_info = mCategory::selectByWhere([
            'id' => $data['category_id']
        ], ['id', 'cate_name(title)']);

        if (empty($category_info)){
            $error = new MessageBag([
                'title' => 'category_id错误'
            ]);
            return redirect('/admin/blackCard/categorysort')->with(compact('error'));
        }

        $data['category_sort'] = (int)$request['category_sort'];
        $data['c_status'] = BlackCategorySort::STATUS_NORMAL;
        if (isset($request['id']) && $request['id']) {
            $data['edittime'] = date('Y-m-d H:i:s', time());
            //修改
            $rs = BlackCategorySort::editData($data, ['id' => $request['id']]);
        } else {
            //添加
            $data['addtime'] = date('Y-m-d H:i:s', time());
            $rs = BlackCategorySort::addData($data);
        }
        if ($rs) {
            $success = new MessageBag([
                'title' => '提交成功'
            ]);
            return redirect('/admin/blackCard/categorysort')->with(compact('success'));
        } else {
            $error = new MessageBag([
                'title' => '提交失败'
            ]);
            return redirect('/admin/blackCard/categorysort')->with(compact('error'));
        }
    }

    //删除
    public function delCategory($id)
    {
        $data = BlackCategorySort::getById($id);
        if ($data) {
            $data['c_status'] = BlackCategorySort::STATUS_DELETE;
            $data['edittime'] = date('Y-m-d H:i:s', time());

            $rs = BlackCategorySort::editData($data, ['id' => $id]);
            if ($rs) {
                return response()->json([
                    'status' => true,
                    'message' => "操作成功",
                ]);
            } else {
                return response()->json([
                    'status' => false,
                    'message' => "操作失败",
                ]);
            }
        } else {
            return response()->json([
                'status' => false,
                'message' => "操作失败",
            ]);
        }
    }

    public function sortCategory(Request $request)
    {
        $request = $request->all();
        $sorts = $request['sort'];
        if ($sorts && is_array($sorts)) {
            foreach ($sorts as $id => $sort) {
                $data['category_sort'] = $sort;
                $data['edittime'] = date('Y-m-d H:i:s', time());
                BlackCategorySort::editData($data, ['id' => $id]);
            }
        }
    }

}

